package com.example.demo.filter;

import jakarta.servlet.*;
import jakarta.servlet.annotation.WebFilter;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.AntPathMatcher;

import java.io.IOException;

//成功xmlHttpRequest.withCredentials=true;需添加在ajax处
@Slf4j
@WebFilter(filterName = "loginFilter", urlPatterns = "/*")//第二个是所有的请求都拦截的意思,第一个是过滤器名字,要和springmvcconfig一起使用
//Filter比controller高一层(外面),所以先执行
@Configuration
public class LoginFilter implements Filter {
    //路径匹配器
    public static final AntPathMatcher PATH_MATCHER = new AntPathMatcher();//写给静态页面路径匹配的

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
//1、获取本次请求的URI
        String requestURI = request.getRequestURI();
        log.info("拦截到请求：{}", requestURI);



        //post请求不拦截,因为post请求携带不了session(前端代码的问题)
        if (request.getMethod().contains("POST")) {
            System.out.println("post请求,放行");
            filterChain.doFilter(request, response);
            return;
        }
//注释掉我正常拦截
        if (true){
            System.out.println("所有请求放行");
            filterChain.doFilter(request, response);
            return;
        }




        //定义不需要处理的请求路径,因为静态页面没有数据
        String[] urls = new String[]{
//                "/",
                "/webapp/*",
                "/webapp/*/*",
                "/test/*",
                "/login%20admin",
                "/login%20company",//这2个使管理员端的账户管理存在bug
                "/login%20stu",//这2个使管理员端的账户管理存在bug
                "/setAccount",
                "/getAccount",
        };
//        2、判断本次请求是否需要处理
        boolean check = check(urls, requestURI);

//        3、如果不需要处理，则直接放行
        if (check) {
            log.info("本次请求{}不需要处理", requestURI);
            filterChain.doFilter(request, response);
            return;
        }


        //4、判断登录状态，如果已登录，则直接放行(session问题未解决)
        if (request.getSession().getAttribute("user") != null) {
            log.info("用户已登录，用户id(账号)为：{}", request.getSession().getAttribute("user"));
            System.out.println("放行");

//            Long userId = (Long) request.getSession().getAttribute("user");
//            BaseContext.setCurrentId(userId);

            filterChain.doFilter(request, response);
            return;
        }


//        filterChain.doFilter(request,response);//放行

        return;
    }

    //静态页面路径匹配,但是这里是登录匹配
    public boolean check(String[] urls, String requestURI) {
        for (String url : urls) {
            boolean match = PATH_MATCHER.match(url, requestURI);
            if (match) {
                return true;
            }
        }
        return false;
    }
}
